package com.tigercel.xiangchengyun.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.entity.NewsReportBean
import com.tigercel.xiangchengyun.utils.Global
import com.tigercel.xiangchengyun.utils.Global.context
import kotlinx.android.synthetic.main.item_news_report.view.*
import org.jetbrains.anko.backgroundResource
import org.jetbrains.anko.textColor


class NewsReportAdapter(private val news: MutableList<NewsReportBean>,
                        private val itemClick: (NewsReportBean) -> Unit) :
        RecyclerView.Adapter<NewsReportAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder? {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_news_report, parent, false)
        return ViewHolder(view, itemClick)
    }

    override fun getItemCount(): Int = news.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindNewsReport(news[position])

    }

    class ViewHolder(view: View, private val itemClick: (NewsReportBean) -> Unit) :
            RecyclerView.ViewHolder(view) {
        fun bindNewsReport(newsReportBean: NewsReportBean) {
            with(newsReportBean) {
                val options = RequestOptions()
                options.placeholder(R.drawable.img_report)
                        .error(R.drawable.ic_empty)
                if (thumb_img.isEmpty()) {
//                    Log.e("图片为空", "空的图片数组")
                    itemView.news_summary_photo_iv_group.visibility = View.INVISIBLE
                    itemView.news_summary_photo_iv_left.visibility = View.INVISIBLE
                    itemView.news_summary_photo_iv_middle.visibility = View.INVISIBLE
                    itemView.news_summary_photo_iv_right.visibility = View.INVISIBLE
                } else if (thumb_img.size == 1) {
//                    Log.e("图片数量", thumb_img.size.toString())
                    itemView.news_summary_photo_iv_group.visibility = View.VISIBLE
                    itemView.news_summary_photo_iv_left.visibility = View.VISIBLE
                    itemView.news_summary_photo_iv_middle.visibility = View.INVISIBLE
                    itemView.news_summary_photo_iv_right.visibility = View.INVISIBLE
                    Glide.with(context)
                            .load(Global.ImgBaseUrl + thumb_img[0])
                            .apply(options)
                            .into(itemView.news_summary_photo_iv_left)

                } else if (thumb_img.size == 2) {
//                    Log.e("图片数量", thumb_img.size.toString())
                    itemView.news_summary_photo_iv_group.visibility = View.VISIBLE
                    itemView.news_summary_photo_iv_group.visibility = View.VISIBLE
                    itemView.news_summary_photo_iv_middle.visibility = View.VISIBLE
                    itemView.news_summary_photo_iv_right.visibility = View.INVISIBLE
                    Glide.with(context)
                            .load(Global.ImgBaseUrl + thumb_img[0])
                            .apply(options)
                            .into(itemView.news_summary_photo_iv_left)
                    Glide.with(context)
                            .load(Global.ImgBaseUrl + thumb_img[1])
                            .apply(options)
                            .into(itemView.news_summary_photo_iv_middle)
                } else {
//                    Log.e("图片数量", thumb_img.size.toString())
                    itemView.news_summary_photo_iv_group.visibility = View.VISIBLE
                    itemView.news_summary_photo_iv_group.visibility = View.VISIBLE
                    itemView.news_summary_photo_iv_middle.visibility = View.VISIBLE
                    itemView.news_summary_photo_iv_right.visibility = View.VISIBLE
                    Glide.with(context)
                            .load(Global.ImgBaseUrl + thumb_img[0])
                            .apply(options)
                            .into(itemView.news_summary_photo_iv_left)
                    Glide.with(context)
                            .load(Global.ImgBaseUrl + thumb_img[1])
                            .apply(options)
                            .into(itemView.news_summary_photo_iv_middle)
                    Glide.with(context)
                            .load(Global.ImgBaseUrl + thumb_img[2])
                            .apply(options)
                            .into(itemView.news_summary_photo_iv_right)
                }
                itemView.tv_type.text = model
                if (itemView.tv_type.text == "") {
                    itemView.tv_type.visibility = View.GONE
                }
//                Log.e("状态", isHandle.toString())
                itemView.title.text = title
                itemView.time.text = pub_time
                itemView.tv_look_up.text = "$lookUp" + "浏览"
                itemView.tv_handle.text = if (isHandle) "已处理" else "待处理"
                if (itemView.tv_handle.text == "已处理") {
                    itemView.tv_handle.backgroundResource = R.drawable.handle_bg_shape_green
                    itemView.tv_handle.textColor = R.color.handle_text_color
                }
                itemView.setOnClickListener { itemClick(this) }
            }
        }

    }

    fun clear() {
        news.clear()
        notifyDataSetChanged()
    }

    fun addAll(data: MutableList<NewsReportBean>) {
        news.addAll(data)
        notifyDataSetChanged()
    }

}